{# layout.html.twig #}
{# Base layout #}

{% set sPortalName = ('portal:' ~ app['combodo.portal.instance.id']) |dict_s %}

{% if app['combodo.current_user'] is defined and app['combodo.current_user'] is not null %}
	{% set bUserConnected = true %}
    {% set bUserCanLogOff = app['combodo.current_user'].CanLogOff() %}
	{% set sUserFullname = app['combodo.current_user'].Get('first_name') ~ ' ' ~ app['combodo.current_user'].Get('last_name') %}
	{% set sUserEmail = app['combodo.current_user'].Get('email') %}
	{% set sUserPhotoUrl = app['combodo.current_contact.photo_url'] %}
{% else %}
	{% set bUserConnected = false %}
    {% set bUserCanLogOff = false %}
	{% set sUserFullname = '' %}
	{% set sUserEmail = '' %}
	{% set sUserPhotoUrl = app['combodo.portal.base.absolute_url'] ~ 'img/user-profile-default-256px.png' %}
{% endif %}

<!doctype html>
<html lang="{{ app['combodo.portal.metadata.language'] }}">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	{# This block can be used to add your own meta tags by extending the default template #}
	{% block pPageExtraMetas %}
	{% endblock %}
	<title>{% block pPageTitle %}{% if sPageTitle is defined and sPageTitle is not null %}{{ sPageTitle }} - {{ constant('ITOP_APPLICATION_SHORT') }}{% else %}{{ 'Page:DefaultTitle'|dict_format(constant('ITOP_APPLICATION_SHORT')) }}{% endif %}{% endblock %}</title>
    <link rel="shortcut icon" href="{{ app['combodo.portal.instance.conf'].properties.favicon|add_itop_version }}"/>

	{% block pPageStylesheets %}
        {# First bootstrap core, lib themes, then bootstrap theme, portal adjustements #}
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/bootstrap/css/bootstrap.min.css'|add_itop_version }}" rel="stylesheet">
        {# - Bootstrap Datetime picker #}
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css'|add_itop_version }}" rel="stylesheet">
        {# - Datatables #}
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/datatables/css/dataTables.bootstrap.min.css'|add_itop_version }}" rel="stylesheet">
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/datatables/css/fixedHeader.bootstrap.min.css'|add_itop_version }}" rel="stylesheet">
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/datatables/css/responsive.bootstrap.min.css'|add_itop_version }}" rel="stylesheet">
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/datatables/css/scroller.bootstrap.min.css'|add_itop_version }}" rel="stylesheet">
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/datatables/css/select.bootstrap.min.css'|add_itop_version }}" rel="stylesheet">
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/datatables/css/select.dataTables.min.css'|add_itop_version }}" rel="stylesheet">
        {# - Font OpenSans #}
        <link href="{{ app['combodo.absolute_url'] ~ 'css/font-open-sans/font-open-sans.css'|add_itop_version }}" rel="stylesheet">
        {# - Font Combodo #}
        <link href="{{ app['combodo.absolute_url'] ~ 'css/font-combodo/font-combodo.css'|add_itop_version }}" rel="stylesheet">
        {# - Font awesome #}
        <link href="{{ app['combodo.absolute_url'] ~ 'css/font-awesome/css/all.min.css'|add_itop_version }}" rel="stylesheet">
        {# - Misc libs #}
        <link href="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/typeahead/css/typeaheadjs.bootstrap.css'|add_itop_version }}" rel="stylesheet">
        <link href="{{ app['combodo.absolute_url'] ~ 'css/selectize.default.css'|add_itop_version }}" rel="stylesheet">
        <link href="{{ app['combodo.absolute_url'] ~ 'node_modules/magnific-popup/dist/magnific-popup.css'|add_itop_version }}" rel="stylesheet">
		<link href="{{ app['combodo.absolute_url'] ~ 'node_modules/c3/c3.min.css'|add_itop_version }}" rel="stylesheet">
		{# - Bootstrap theme #}
		<link href="{{ app['combodo.absolute_url'] ~ app['combodo.portal.instance.conf'].properties.themes.bootstrap|add_itop_version }}" rel="stylesheet" id="css_bootstrap_theme">
		{# - Portal adjustments for BS theme #}
		<link href="{{ app['combodo.absolute_url'] ~ app['combodo.portal.instance.conf'].properties.themes.portal|add_itop_version }}" rel="stylesheet" id="css_portal">
        {# - New main portal file #}
        <link href="{{ app['combodo.absolute_url'] ~ app['combodo.portal.instance.conf'].properties.themes.main|add_itop_version }}" rel="stylesheet" id="css_portal">
		{# Tippy for tooltips and Popper for their placement #}
		<link href="{{ app['combodo.absolute_url'] ~ 'node_modules/tippy.js/dist/tippy.css'|add_itop_version }}" rel="stylesheet">
		<link href="{{ app['combodo.absolute_url'] ~ 'node_modules/tippy.js/animations/shift-away-subtle.css'|add_itop_version }}" rel="stylesheet">
        {# Fontawesome 5 power transform as CSS #}
		<link href="{{ app['combodo.absolute_url'] ~ 'node_modules/@cityssm/fa5-power-transforms-css/fa5-power-transforms.min.css'|add_itop_version }}" rel="stylesheet">
		{# UI Extensions CSS, in an undefined order #}
        {% if app['ui_extensions_helper'].css_files is defined %}
            {% for css_file in app['ui_extensions_helper'].css_files %}
				<link href="{{ css_file|add_itop_version }}" rel="stylesheet">
            {% endfor %}
        {% endif %}
		{# Custom CSS that is supposed to do adjustments to the portal #}
		{% if app['combodo.portal.instance.conf'].properties.themes.custom is defined %}
			<link href="{{ app['combodo.absolute_url'] ~ app['combodo.portal.instance.conf'].properties.themes.custom|add_itop_version }}" rel="stylesheet">
		{% endif %}
		{# Others CSS that will come after the theme/portal/custom, in an undefined order #}
		{% if app['combodo.portal.instance.conf'].properties.themes.others is defined %}
			{% for theme in app['combodo.portal.instance.conf'].properties.themes.others %}
				<link href="{{ app['combodo.absolute_url'] ~ theme|add_itop_version }}" rel="stylesheet">
			{% endfor %}
		{% endif %}
	{% endblock %}

	{% block pStyleinline %}
        {# UI Extensions inline CSS #}
        {% if app['ui_extensions_helper'].css_inline is not null %}
            <style>
                {{ app['ui_extensions_helper'].css_inline|raw }}
            </style>
        {% endif %}
	{% endblock %}

	{% block pPageScripts %}
		<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/jquery/dist/jquery.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/ajax_hook.js'|add_itop_version }}"></script>
		{% if is_development_environment() %}
				<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/jquery-migrate/dist/jquery-migrate.js'|add_itop_version }}"></script>
		{% else %}
				<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/jquery-migrate/dist/jquery-migrate.min.js'|add_itop_version }}"></script>
        {% endif %}
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/jquery-ui-dist/jquery-ui.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/magnific-popup/dist/jquery.magnific-popup.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/blueimp-file-upload/js/jquery.iframe-transport.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/blueimp-file-upload/js/jquery.fileupload.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/utils.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/toolbox.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/image_toolkit.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/d3/d3.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/c3/c3.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/bootstrap/js/bootstrap.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/bootstrap-patches.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/bootstrap-portal-modal.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/latinise/latinise.min.js'|add_itop_version }}"></script>
        {# Tippy for tooltips and Popper for their placement #}
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/@popperjs/core/dist/umd/popper.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/tippy.js/dist/tippy-bundle.umd.js'|add_itop_version }}"></script>
        {# Visible.js to check if an element is visible on screen #}
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/jquery-visible/js/jquery.visible.min.js'|add_itop_version }}"></script>
        {# Base64.js #}
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/jquery-base64/js/jquery.base64.min.js'|add_itop_version }}"></script>
        {# Moment.js with locales #}
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/moment/min/moment-with-locales.min.js'|add_itop_version }}"></script>
        {# Datatables #}
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/datatables.net/js/jquery.dataTables.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/datatables/js/dataTables.bootstrap.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/datatables.net-responsive/js/dataTables.responsive.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/datatables.net-scroller/js/dataTables.scroller.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/datatables.net-select/js/dataTables.select.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/datatables/js/datetime-moment.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/dataTables.accentNeutraliseForFilter.js'|add_itop_version }}"></script>
        {# Export for Datatables #}
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/export.js'|add_itop_version }}"></script>
        {# CKEditor files for HTML WYSIWYG #}
        {{ inject_ckeditor_resources()|raw }}
        {# - Highlighter for code snippets created with CKEditor #}
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/highlight/highlight.min.js'|add_itop_version }}"></script>
        {# Date-time picker for Bootstrap #}
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js'|add_itop_version }}"></script>
        {# Typeahead files for autocomplete #}
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/typeahead/js/typeahead.bundle.min.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'lib/handlebars/js/handlebars.min-768ddbd.js'|add_itop_version }}"></script>

        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/leave_handler.js'|add_itop_version }}"></script>

        {# Selectize for sets #}
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/selectize.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/jquery.itop-set-widget.js'|add_itop_version }}"></script>
        {# Form files #}
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/form_handler.js'|add_itop_version }}"></script>
		<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/form_field.js'|add_itop_version }}"></script>
		<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/subform_field.js'|add_itop_version }}"></script>
		<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/field_set.js'|add_itop_version }}"></script>
		{# Form files for portal #}
		<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/portal_form_handler.js'|add_itop_version }}"></script>
		<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/portal_form_field.js'|add_itop_version }}"></script>
		<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/portal_form_field_html.js'|add_itop_version }}"></script>
		<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/portal_form_field_set.js'|add_itop_version }}"></script>
		{# Clipboard helper #}
		<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/clipboard/dist/clipboard.min.js'|add_itop_version }}"></script>
		<script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'js/clipboardwidget.js'|add_itop_version }}"></script>
		<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/portal-clipboard.js'|add_itop_version }}"></script>
        {# User Preferences #}
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/user_preferences.js'|add_itop_version }}"></script>
        {# Polyfill for custom elements #}
        <script type="text/javascript" src="{{ app['combodo.absolute_url'] ~ 'node_modules/@ungap/custom-elements/es.js'|add_itop_version }}"></script>
        {# custom elements #}
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/custom_elements/base_element.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/custom_elements/tile_element.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/custom_elements/dropdown_element.js'|add_itop_version }}"></script>
        <script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] ~ 'js/custom_elements/navigation_menu_element.js'|add_itop_version }}"></script>

		{# UI Extensions JS, in an undefined order #}
		{% if app['ui_extensions_helper'].js_files is defined %}
			{% for js_file in app['ui_extensions_helper'].js_files %}
				<script type="text/javascript" src="{{ js_file|add_itop_version }}"></script>
			{% endfor %}
        {% endif %}
	{% endblock %}
</head>
<body class="ipb-body {% block pPageBodyClass %}{% endblock %}" data-gui-type="portal">
	{% block pPageBodyWrapper %}
		
		{% block pNavigationWrapper %}
			{% block pNavigationSideMenuWrapper %}
                {% include template('navigation_menu') %}
			{% endblock %}
		{% endblock %}

        <div class="ipb-main--main-container">
		    {% block pMainWrapper %}
            <div class="ipb-page--main-wrapper">
                <section class="ipb-page--session-messages" id="main-session-messages">
                        {% block pEnvBannerWrapper %}
                            {% if app['combodo.current_environment'] != 'production' %}
                                <div id="envbanner" class="ipb-alert alert alert-danger" role="alert">
                                    {{ 'Portal:EnvironmentBanner:Title'|dict_format( app['combodo.current_environment']|upper )|raw }}
                                    <button class="ipb-button ipb-is-regular ipb-is-default" type="button" onclick="window;location.href='{{ app['url_generator'].generate('p_home', {'switch_env': 'production'}) }}'">
                                        {{ 'Portal:EnvironmentBanner:GoToProduction'|dict_s|raw }}
                                    </button>
                                </div>
                            {% endif %}
                        {% endblock %}

                        {% include template('session_messages') %}
                </section>
                <section class="ipb-page--main-header" id="main-header">
                    {% block pMainHeader %}
                    {% endblock %}
                </section>
                <section class="ipb-page--main-content" id="main-content">
                    {% block pMainContent %}
                    {% endblock %}
                </section>

                {% block pPageUIExtensionMainContent %}
                    {% if app['ui_extensions_helper'].html[constant('iPortalUIExtension::ENUM_PORTAL_EXT_UI_MAIN_CONTENT')] is defined %}
                        {{ app['ui_extensions_helper'].html[constant('iPortalUIExtension::ENUM_PORTAL_EXT_UI_MAIN_CONTENT')]|raw }}
                    {% endif %}
                {% endblock %}
            </div>
		    {% endblock %}
            <footer id="footer-wrapper">
                {% block pPageFooter%}
                    <a href="http://www.combodo.com" class="ipb-copyright">iTop &copy; {{ "now"|date('Y') }}</a>
                {% endblock %}
            </footer>
        </div>

		{% block pModalForAllWrapper %}
			<div class="modal fade" id="modal-for-all" role="dialog" tabindex="-1">
				<div class="modal-dialog modal-lg" role="document">
					<div class="modal-content">
                        {% include template('loader') %}
					</div>
				</div>
			</div>
		{% endblock %}
		{% block pModalForAlert %}
			<div class="modal fade" id="modal-for-alert" role="dialog" tabindex="-1">
				<div class="modal-dialog" role="document">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-label="{{ 'Portal:Button:Close'|dict_s }}"><span aria-hidden="true">&times;</span></button>
							<h4 class="modal-title"></h4>
						</div>
						<div class="modal-body">
							<div class="alert">
							</div>
							<div class="text-right">
								<button type="button" class="btn btn-primary" data-dismiss="modal">{{ 'Portal:Button:Close'|dict_s }}</button>
							</div>
						</div>
					</div>
				</div>
			</div>
		{% endblock %}

		{% block pPageOverlay %}
			<div id="page_overlay" class="global_overlay">
				<div class="overlay_content">
                    {% include template('loader') %}
				</div>
			</div>
		{% endblock %}

		{% block pPageUIExtensionBody %}
			{% if app['ui_extensions_helper'].html[constant('iPortalUIExtension::ENUM_PORTAL_EXT_UI_BODY')] is defined %}
				{{ app['ui_extensions_helper'].html[constant('iPortalUIExtension::ENUM_PORTAL_EXT_UI_BODY')]|raw }}
			{% endif %}
		{% endblock %}
	{% endblock %}
	
	{% block pPageLiveScripts %}
		<script type="text/javascript">
			{% block pPageLiveScriptHelpers %}
				// Helper to get the application root url
				var GetAbsoluteUrlAppRoot = function()
				{
					return '{{ app['combodo.absolute_url'] }}';
				};
				// Note: We might want to expose URLs instead of this kind of stuff... 🤔
				var GetAddSessionMessageUrl = function()
				{
					return '{{ app['url_generator'].generate('p_session_message_add')|raw }}';
				};
                // user preferences object
                const oUserPreferences = new UserPreferences('{{ app['url_generator'].generate('p_preferences_set_preference')|raw }}');
				/**
				 * @param sUrl {string} The URL to append the new param to
				 * @param sParamName {string} Name of the parameter
				 * @param sParamValue {string} Value of the param, needs to be already URL encoded
				 * @return {string} The sUrl parameters with the sParamName / sParamValue append at the right place
				 * @deprecated 3.0.0 N°4176
				 */
				var AddParameterToUrl = function(sUrl, sParamName, sParamValue)
				{
					sUrl += (sUrl.split('?')[1] ? '&':'?') + sParamName + '=' + sParamValue;
					return sUrl;
				};
				// Test is sString is a valid JSON string
				// TODO 3.0.0: Move to CombodoGlobalToolbox and deprecate this one
				var IsJSONString = function(sString)
				{
					try {
						JSON.parse(sString);
					} catch (oException) {
						return false;
					}
					return true;
				};
				// TODO 3.0.0: Move to CombodoPortalToolbox and deprecate this one
				var GetContentLoaderTemplate = function()
				{
					return '<div class="content_loader"><div class="icon glyphicon glyphicon-refresh"></div><div class="message">{{ 'Page:PleaseWait'|dict_s }}</div></div>';
				};
				// TODO 3.0.0: Move to CombodoPortalToolbox and deprecate this one
				var ShowLoginDialog = function()
				{
                    var oModalElem = $('#modal-for-alert').clone();
                    oModalElem.attr('id', '');
                    oModalElem.find('.modal-content .modal-header .modal-title').html('{{ 'Error:HTTP:401'|dict_s|escape('js') }}');
                    oModalElem.find('.modal-content .modal-body .alert').addClass('alert-danger').html('{{ 'Portal:ErrorUserLoggedOut'|dict_s|escape('js') }}');

                    oModalElem.find('.modal-content .modal-body button').replaceWith( $('<button type="button" class="btn btn-primary" onclick="javascript:window.location.reload();">{{ 'UI:LogOff:ClickHereToLoginAgain'|dict_s|escape('js') }}</button>') );

                    oModalElem.appendTo('body');
                    oModalElem.modal('show');
				};
				// TODO 3.0.0: Move to CombodoPortalToolbox and deprecate this one
				var ShowErrorDialog = function(sBody, sTitle)
				{
				    if(sTitle === undefined)
					{
					    sTitle = '{{ 'Error:HTTP:500'|dict_s|escape('js') }}';
					}
				    if(sBody === undefined)
					{
                        sBody = '{{ 'Error:XHR:Fail'|dict_format(constant('ITOP_APPLICATION_SHORT'))|escape('js') }}';
					}
					var oModalElem = $('#modal-for-alert');
                    oModalElem.find('.modal-content .modal-header .modal-title').text(sTitle);
                    oModalElem.find('.modal-content .modal-body .alert').addClass('alert-danger').text(sBody);
                    oModalElem.modal('show');
				};
			{% endblock %}

            $(document).ready(function ()
            {
                const oBodyElem = $('body');

                {% block pPageReadyScripts %}
					// Add proprietary header to identify our XHR calls
					$(document).ajaxSend(function(oEvent, oXHR, oOptions) {
						oXHR.setRequestHeader('X-Combodo-Ajax', 'true');
					});
					// Handle AJAX errors (exceptions (500), logout (401), ...)
					$(document).ajaxError(function(oEvent, oXHR, oSettings, sError){
					    if(oXHR.status === 401)
						{
						    ShowLoginDialog();
						}
                        else if(oXHR.status === 404)
                        {
                            ShowErrorDialog('{{ 'UI:ObjectDoesNotExist'|dict_s|escape('js') }}', '{{ 'Error:HTTP:404'|dict_s|escape('js') }}');
                        }
                      else if(oXHR.status === 0 && oXHR.readyState === 0)
                        {
                            //DO NOTHING the ajax call has been aborted
                        }
                        else if(oXHR.responseText !== undefined && IsJSONString(oXHR.responseText) === true)
                        {
                            var oData = JSON.parse(oXHR.responseText);
                            // Catching AJAX exception with detailed error.
                            if( (oData.error_message !== undefined) && (oData.error_title !== undefined) )
                            {
                                ShowErrorDialog(oData.error_message, oData.error_title);
                            } else
                            {
                                ShowErrorDialog();
                            }
                        } else
                        {
                            ShowErrorDialog();
                        }
                    });
                // All processing that should be done on an ajax return
                $(document).ajaxSuccess(function ()
                {
                    // Init tooltips from async. markup, small timeout to allow markup to be built if necessary
                    setTimeout(function ()
                    {
                        CombodoTooltip.InitAllNonInstantiatedTooltips();
                    }, 1000);
                });

                // Enable tooltips based on existing HTML markup, for markup added dynamically after DOM ready (AJAX, ...) see .ajaxSuccess()...
                CombodoTooltip.InitAllNonInstantiatedTooltips();
                // ... object form which are handled with the following
                oBodyElem.on('form_built', function (oEvent)
                {
                    CombodoTooltip.InitAllNonInstantiatedTooltips($(oEvent.target), true);
                });
                // ... refresh of the dataTables
                oBodyElem.on('init.dt draw.dt', function (oEvent)
                {
                    CombodoTooltip.InitAllNonInstantiatedTooltips($(oEvent.target), true);
                });

                // Initialize confirmation message handler when a form with touched fields is closed
                oBodyElem.leave_handler({
                    'message': '{{ 'Portal:Form:Close:Warning'|dict_s }}',
                    'extra_events': {
                        'body': ['hide.bs.modal']
                    }
                });
                {% endblock %}
			});
		</script>
	{% endblock %}

	{% block pPageExtensionsScripts %}
        {# UI Extensions inline JS #}
        {% if app['ui_extensions_helper'].js_inline is not null %}
			<script type="text/javascript">
				{{ app['ui_extensions_helper'].js_inline|raw }}
			</script>
        {% endif %}
	{% endblock %}
</body>
</html>
